<?php

class PageListEvents extends Page
{
	public function getHTML()
	{
		$html = $this->getHeaderHTML();

		$submitScript = new SubmitFormScript();
		$html .= $submitScript->getHTML();

		$script = new EditDeleteFormScript();
		$html .= $script->getHTML();

		$html .= $this->getBodyTopHTML();
		$html .= $this->getHiddenVariables();

		$View = new Hidden();
		$View->setName("View");
		$View->setValue('List');
		$html .= $View->gethtml();

		$View = new Hidden();
		$View->setName("CallingPage");
		$View->setValue('ListEvents');
		$html .= $View->gethtml();



		$html .= "<h1>You are logged on - welcome " .$this->getUser()."</h1>\n";

		$aMenu = new MenuSNAP();
		$html .= $aMenu->getHTML();
		$html .= "<br>\n";

		$datatable = new DataTable();
		$datatable->setParentPage($this);
		$datatable->setName("Events");
		$datatable->setValue("Events");
		$datatable->setEditable(true);
		$datatable->setDeletable(true);
/*		if(isset($_POST['ShowDeleted']))
			$datatable->showDeleted($_POST['ShowDeleted']);
		if(isset($_POST['ShowBefore']))
			$datatable->showBefore($_POST['ShowBefore']);
		if(isset($_POST['ShowAfter']))
			$datatable->showAfter($_POST['ShowAfter']);
		elseif((!isset($_POST['ShowBefore']))&&(!isset($_POST['ShowAfter'])))
			$datatable->showAfter(true);
		if(isset($_POST['ShowLocations']))
			$datatable->showLocations(true);
		if(isset($_POST['ShowEventInvolvees']))
			$datatable->showEventInvolvees(true);
		if(isset($_POST['ShowEventTypes']))
			$datatable->showEventTypes(true);*/
		$datatable->setDBTableName("Events");
		$datatable->setDatabase($this->Database);
		$theRenderer = new DataTableRendererEdit($datatable);
		$theRenderer->setAction = "ListEvents";
		$datatable->setRenderer($theRenderer);
		$columns = array();
		$columns[] = new DataTableColumn("Start","Date","DateOnly");
		$columns[] = new DataTableColumn("Start","Start","TimeOnly");
		$columns[] = new DataTableColumn("End","End","TimeOnly");
		$columns[] = new DataTableColumn("Title","Title","");
		$columns[] = new DataTableColumn("Description","Description","");
		$columns[] = new DataTableColumn("EventTypeID","Event Type","SingleEnumeration");
		$columns[] = new DataTableColumn("EventInvolvee","Event Involves ...","MultiEnumeration");
		$columns[] = new DataTableColumn("Location","Location","MultiEnumeration");
		$datatable->setColumns($columns);
		$datatable->setSelect("select EventID as ColumnID, Start, End, Title, Description, EventTypeID, DeletedBy FROM Events");
		$datatable->setOrderBy("Start");

// Add filter requests

// Deleted
		$aFilterRequest = new FilterRequest();
		$aFilterRequest->setName("frShowDeleted");
		$aFilterRequest->setFilterType("Checkbox");
		$aFilterRequest->setText("Show Deleted");
		$aFilterRequest->setField("DeletedBy");
		$aFilterRequest->setValue("");
		$aFilterRequest->setInvertEnabled(true);
		$aFilterRequest->setIsORCondition(false);
		$isEnabled = false;
		if(isset($_POST['frShowDeleted']))
			$isEnabled = ($_POST['frShowDeleted']=="on");
		$aFilterRequest->setEnabled($isEnabled);
		$datatable->addFilterRequest($aFilterRequest);

// Time

		$dtNow = new DateTime("now");
		$valueNow = $dtNow->format("Y-m-d H:i");

		$aFilterRequest = new FilterRequest();
		$aFilterRequest->setName("frShowBefore");
		$aFilterRequest->setFilterType("Date");
		$aFilterRequest->setText("Show Before");
		$aFilterRequest->setField("Start");
		$aFilterRequest->setValue($valueNow);
		$aFilterRequest->setIsORCondition(true);
		$isEnabled = false;
		if(isset($_POST['frShowBefore']))
			$isEnabled = ($_POST['frShowBefore']=="on");
		$aFilterRequest->setEnabled($isEnabled);
		$aFilterRequest->setSelectAfter(false);
		$datatable->addFilterRequest($aFilterRequest);

		$aFilterRequest = new FilterRequest();
		$aFilterRequest->setName("frShowAfter");
		$aFilterRequest->setFilterType("Date");
		$aFilterRequest->setText("Show After");
		$aFilterRequest->setField("Start");
		$aFilterRequest->setValue($valueNow);
		$aFilterRequest->setIsORCondition(true);
		$isEnabled = true;
		if(isset($_POST['frShowAfter']))
			$isEnabled = ($_POST['frShowAfter']=="on");
		$aFilterRequest->setEnabled($isEnabled);
		$datatable->addFilterRequest($aFilterRequest);



/*
//SET CONDITIONS

// Deleted
		$aCondition = ($datatable->getShowDeleted())?"":" DeletedBy = ''";
		$datatable->addANDCondition($aCondition);

// Time
		$aCondition = "";
		$dtNow = new DateTime("now");
		$showAfter = (isset($_POST["dtShowAfter"]))?$_POST["dtShowAfter"]:$dtNow->format("Y-m-d H:i");
		$showBefore = (isset($_POST["dtShowBefore"]))?$_POST["dtShowBefore"]:$dtNow->format("Y-m-d H:i");
		if($datatable->getShowBefore() && $datatable->getShowAfter())$aCondition = "";
		if(!$datatable->getShowBefore() && $datatable->getShowAfter())$aCondition = "End >= '".substr($showAfter,0,10)."' ";
		if($datatable->getShowBefore() && !$datatable->getShowAfter())$aCondition = "Start <= '".substr($showBefore,0,10)."' ";
		if(!$datatable->getShowBefore() && !$datatable->getShowAfter())$aCondition = " 0 = 1";
		$datatable->addANDCondition($aCondition);

// Event Type
	//($_POST['dtEventType']!=$row['EventTypeID'])
		if($datatable->getShowEventTypes())
			$datatable->addANDCondition(" EventTypeID = ".$_POST['dtEventType']);

// Location

		if($datatable->getShowLocations())
		{
			$locationSQL = "(";
			foreach($_POST['dtLocation'] as $locationID)
			{
				$locationSQL .= "(EventID in (SELECT EventID from Locations where LocationNameID = ".$locationID.")) OR ";
			}
			$locationSQL = substr($locationSQL,0,-3).") ";
			$datatable->addANDCondition($locationSQL);
		}


// Event Involvees

		if($datatable->getShowEventInvolvees())
		{
			$involveeSQL = "(";
			foreach($_POST['dtEventInvolvee'] as $involveeID)
			{
				$involveeSQL .= "(EventID in (SELECT EventID from EventInvolvees where EventInvolveeNameID = ".$involveeID.")) OR ";
			}
			$involveeSQL = substr($involveeSQL,0,-3).") ";
			$datatable->addANDCondition($involveeSQL);
		}

*/


		$html .= $datatable->getHTML();
		$html .= "<br>\n";

		$html .= $this->getBodyBottomHTML();
		return $html;
	}
}
?>